"""
读取摄像头数据
处理数据并且展示数据
"""
import cv2


capture = cv2.VideoCapture(0)

# 获取 capture 的一些属性
frame_width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv2.CAP_PROP_FPS)
print(frame_width, frame_height, fps)

if capture.isOpened() is False:
    print('Error openning the camera')

while capture.isOpened():
    ret, frame = capture.read()

    if ret:
        # 显示摄像头捕获的帧
        cv2.imshow('Input frame from the camera', frame)
        # 把摄像头捕捉到的帧转换为灰度
        gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        # 显示处理后的帧
        cv2.imshow('Grayscale input camera', gray_frame)
        # cv2.waitKey()这个函数是在一个给定的时间内(单位ms)等待用户按键触发
        # 如果用户没有按下按键，则继续等待(循环)
        if (cv2.waitKey(10) & 0xFF) == ord('q'):
            break
    else:
        break

capture.release()
cv2.destroyAllWindows()